(function() {
  var crypto, iced, mailer, moment, pgsql, sendmail, __iced_k, __iced_k_noop;

  iced = require('iced-runtime');
  __iced_k = __iced_k_noop = function() {};

  mailer = require('nodemailer');

  pgsql = require('pg');

  crypto = require('crypto');

  moment = require('moment');

  sendmail = function(params) {
    var client, config, decipher, done, err, mail, passwd, record, result, secure_key, transporter, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/lib/sendmail.coffee"
        });
        pgsql.connect(settings.pgsql_url, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              client = arguments[1];
              return done = arguments[2];
            };
          })(),
          lineno: 18
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        if (err) {
          return done(client) && console.log(err);
        }
        (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/wuxiaohu/lucky-byte/luckpay-web/coffee/lib/sendmail.coffee"
          });
          client.query("select * from web_email limit 1", __iced_deferrals.defer({
            assign_fn: (function() {
              return function() {
                err = arguments[0];
                return result = arguments[1];
              };
            })(),
            lineno: 21
          }));
          __iced_deferrals._fulfill();
        })(function() {
          if (err) {
            return done(client) && console.log(err);
          }
          done();
          if (result.rows.length === 0) {
            return console.error('邮件服务未配置，不能发送电子邮件');
          }
          record = result.rows[0];
          secure_key = Buffer.from(settings.secure_key, 'hex');
          if (secure_key.length !== 16) {
            return console.error('安全密钥配置错误，必须由 32 位[0-9A-F]数字组成');
          }
          decipher = crypto.createDecipheriv('des-ede', secure_key, new Buffer(''));
          passwd = decipher.update(record.smtp_passwd, 'hex', 'utf8');
          passwd += decipher.final('utf8');
          config = {
            logger: false,
            debug: false,
            host: record.smtp_host,
            port: record.smtp_port,
            secure: record.smtp_ssl,
            tls: {
              rejectUnauthorized: false
            },
            auth: {
              user: record.smtp_from,
              pass: passwd
            }
          };
          mail = {
            subject: params.subject || '无主题',
            from: "\"" + record.smtp_name + "\" " + record.smtp_from,
            bcc: "x@lucky-byte.com",
            to: params.to,
            cc: params.cc,
            html: params.html,
            text: params.text
          };
          transporter = mailer.createTransport(config);
          return transporter.sendMail(mail);
        });
      };
    })(this));
  };

  module.exports = sendmail;

}).call(this);
